<extend name="layout/edit" />

<block name="HEADER">

</block>

<block name="CONTENT">
    <control:form id="form" />
</block>

<block name="FOOTER">
   <script type="text/javascript">
    var  page=(function(ns){
    	
		ns.init = function(){
			if(__mode ==3 ){
				form.setBtn('btnSave',false);   
			}
		}
		
		//获取总库存
		ns.getTotalStock = function(){
			var totalStock = eval($.map(form.$grids['specsGrid'].getRows(),function(h){
				return h.stock || 0;
			}).join('+'));
			form.setValue('stocks', totalStock.toFixed(0) );
		}
		
        
        //保存
        ns.save=function(){
        	var param = form.getData();
        	if(!param) return false;
        	if(!param.cate_id){
        		return $.toast(0,'请选择所属分类！');
        	};
        	if(!param.img){
        		return $.toast(0,'请上传商品主图！');
        	};
        	if(!param.banners){
        		return $.toast(0,'请上传商品轮播图！');
        	};
        	form.save(4).then(function(res){
        		var specsData = {
	        		goods_id:res.data,
	        		name:'默认规格',
	        		is_default:1,
	        		stock : param.stock,
	        		cost_price : param.cost_price,
	        		price : param.cost_price,
	        		sale_price : param.sale_price,
	        		use_integral : param.need_integral,
	        		status:1
	        	};
	        	if(param.specs_id) specsData.id = param.specs_id
        		$.ajaxs('/admin/v1/goods/addOrUpdateChilds',{
        			goods_id:res.data,
        			goodsData:JSON.stringify([]),
	        		specsData: JSON.stringify([specsData]),
	        	}).then(function(){
					form.opener.grid.load();
					form.opener.$.toast(1,"保存成功！");
					if(window.__mode==1){
						$.setRequest({oid:res.data,mode:2});
					}else{
						form.$grids['setmealGrid'].load();
						form.$grids['specsGrid'].load();
					}
	    		});
        	});
        }
        return ns;
    }({}));
    
    $(function(){ page.init(); });
   </script>
</block>



